#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xDD, 0xEE, 0xFE, 0xED };
//IPAddress ip(10,0,0,177);
byte ip[] = { 10, 0, 0, 43 };    
byte subnet[] = { 255, 255, 255, 0 };    

EthernetServer server(334);

String inputString = "";         // a string to hold incoming data

void setup() {
//  for(int i =2; i < 13; i++){
//    pinMode(i,OUTPUT);  
//  }
pinMode(53,OUTPUT);
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
pinMode(50,OUTPUT);
digitalWrite(50,HIGH);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac,ip, subnet);
  server.begin();
//  Serial.begin(9600);
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
//    Serial.println("Client Connected");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char inChar = (char)client.read();
          if (inChar == '\n') {

          client.println("Thankyou");
          int i, count; 
          for (i=0, count=0; inputString[i]; i++) 
            count += (inputString[i] == ','); 

          String newData[count];

          for(int i =0; i < count;i++){
            int firstpos = inputString.indexOf(",");
            newData[i] = inputString.substring(0,firstpos);
            inputString = inputString.substring(firstpos+1,inputString.length());
          }

//          for(int i =2; i < 13; i++){
//             analogWrite(i,convlong(newData[i-2]));
//          }

          inputString = "";
        } 
        else{
          inputString += inChar;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}


long convlong(String Data){
  char carray2[Data.length() +1];
  Data.toCharArray(carray2,sizeof(carray2));
  return atol(carray2); 
}
